import React, { Component } from "react";
import ChildComp1 from "./ChildComp1";

export default class ParentComp extends Component {
  constructor(props) {
    super();
    this.comRef = React.createRef();
  }

  clickHandle = () => {
    console.log(this);
    console.log(this.comRef); // {current: ChildCom1}
    this.comRef.current.test();
  };

  render() {
    return (
      <div>
      <h2>除了在 *JSX* 中关联 *Ref*，我们还可以直接关联一个类组件，这样就可以直接调用该组件内部的方法</h2>
        {/* ref 关联子组件 */}
        <ChildComp1 ref={this.comRef} />
        <button onClick={this.clickHandle}>触发子组件方法</button>
      </div>
    );
  }
}
